package org.techmore.biz;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.techmore.utils.DatabaseHelper;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.AllCouponSeriesVO;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WelcomeActivityDataHandler {

	private static WelcomeActivityDataHandler welcomeActivityDataHandler = new WelcomeActivityDataHandler();
	
	public static WelcomeActivityDataHandler getInstance(){
		return welcomeActivityDataHandler;
	}
	
	
	public static List<AllCouponSeriesVO> allCouponSeriesVOList = new ArrayList<AllCouponSeriesVO>();
	public void insertXmlToSqlite(File xmlFile,Context context){
		parseXml(xmlFile);
		if(allCouponSeriesVOList.size()>0){
			String databaseName = PropertiesUtil.getInstance().getValue("databaseName");
			SQLiteDatabase sqLiteDatabase = null;
			try {
				sqLiteDatabase = new DatabaseHelper(context, databaseName).getWritableDatabase();
				sqLiteDatabase.beginTransaction();
				
				//清空表内容
				sqLiteDatabase.execSQL("delete from allCouponSeries");
				
				StringBuffer sb = null;
				for(AllCouponSeriesVO vo : allCouponSeriesVOList){
					 sb = new StringBuffer();
					 sb.append("insert into allCouponSeries (couponSeriesNo,store,subject,img48xUrl) values ");
					 sb.append(" (?,?,?,?)");
					 sqLiteDatabase.execSQL(sb.toString(),
							 new String[]{vo.getCouponSeriesNo(),vo.getStore(),vo.getSubject(),vo.getImg48xUrl()});
				}
				sqLiteDatabase.setTransactionSuccessful();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.e("sqlite", e.getMessage());
			}finally{
				sqLiteDatabase.endTransaction();
				sqLiteDatabase.close();
			}
		}
	}



	private void parseXml(File xmlFile) {
		if(allCouponSeriesVOList.size()>0){
			allCouponSeriesVOList.clear();
		}
		if(xmlFile.exists()){
			try {
				SAXParserFactory factory = SAXParserFactory.newInstance();
				SAXParser parser = factory.newSAXParser();
				parser.parse(xmlFile, new AllCouponSeriesVO_Handler(allCouponSeriesVOList));
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	public class AllCouponSeriesVO_Handler extends DefaultHandler{
		private AllCouponSeriesVO allCouponSeriesVO = null;
		private List<AllCouponSeriesVO> allCouponSeriesVOList = null;
		public AllCouponSeriesVO_Handler(List<AllCouponSeriesVO> allCouponSeriesVOList){
			this.allCouponSeriesVOList = allCouponSeriesVOList;
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
		}
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			
			if("coupon".equals(qName) || "coupon".equals(localName) ){
				allCouponSeriesVO = new AllCouponSeriesVO();
				allCouponSeriesVO.setCouponSeriesNo(attributes.getValue("couponSeriesNo"));
				allCouponSeriesVO.setImg48xUrl(attributes.getValue("img48xUrl"));
				allCouponSeriesVO.setStore(attributes.getValue("store"));
				allCouponSeriesVO.setSubject(attributes.getValue("subject"));
				allCouponSeriesVOList.add(allCouponSeriesVO);
			}
		}
		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			super.endElement(uri, localName, qName);
			if("coupon".equals(qName) || "coupon".equals(localName)){
				allCouponSeriesVO = null;
			}
		}
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
		}
	}
}
